﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Top.Web;
using Top.Data;
using Top.Web.Api;
namespace Top.PointStore.Business
{
    /*功能说明:经纪人操作管理类
     *版本:
     *参与人:王士锋
     *修改日期:2015.3.4
     */
    public class BrokerManager
    {
        /// <summary>
        /// 判断经纪人是否存在
        /// </summary>
        /// <param name="brokerKid"></param>
        /// <returns></returns>
        public static async Task<int> IsExists(int brokerKid)
        {
            if (brokerKid <= 0) return -1;
            const string sql = "select count(1) from T_Broker where Kid={0}";
            using (var db = new DbTour())
            {
                return await db.Sql(sql, brokerKid).ExecuteScalar<int>(0);
            }
        }


        /// <summary>
        /// 根据经纪人id获取经纪人信息
        /// </summary>
        /// <param name="brokerKid"></param>
        /// <returns></returns>
        public static async Task<T_Broker> Get(int brokerKid)
        {
            if (brokerKid <= 0) return null;
            const string sql = "select * from T_Broker where Kid={0} limit 1";
            using (var db = new DbTour())
            {
                return await db.Sql(sql, brokerKid).FirstOrDefault<T_Broker>();
            }
        }

        public static async Task<T_Broker> Get(string userName)
        {
            if (string.IsNullOrEmpty(userName))
            {
                return null;
            }
//            const string sql = @"SELECT b.*
//	                        ,c.F_IsDistribute
//	                        ,p.F_Name as F_CityName
//                        FROM T_Broker AS b
//                        LEFT JOIN T_BrokerCompany AS c ON c.Kid = b.F_BrokerCompanyKid
//                        left join T_ProvinceCityArea as p on b.F_CityKid=p.Kid
//                        WHERE b.Kid = (select Kid from T_Broker where F_Phone={0} limit 1) limit 1";
            const string sql = @"SELECT
                        	     kid,
			                    F_Title,
			                    F_WX_Nickname,
			                    F_Phone                   
                        FROM T_Broker AS b
                        WHERE F_Phone={0} 
                        limit 1";
            using (var db = new DbTour())
            {
                return await db.Sql(sql, userName).FirstOrDefault<T_Broker>();
            }
        }
        public static async Task<bool> IsStar(OAuthUser User)
        {
            if(User!=null)
            {
                const string sql = @" SELECT a.F_StarStatus FROM t_broker a JOIN t_oauthinfo b ON a.Kid=b.F_BrokerKid WHERE b.F_BrokerKid!=0 AND b.F_UserToken={0}";
                using(var db= DataBaseContext.GetDataBase("reader"))
                {
                    var rel = await db.Sql(sql, User.UserToken).FirstOrDefault<int>();
                    return rel == 1;
                }
            }
            return false;
        }
    }
}
